//
//  FYWelComeViewController.swift
//  Mircore
//
//  Created by apple on 16/8/10.
//  Copyright © 2016年 飞羽. All rights reserved.
//

import UIKit

import SnapKit

import SDWebImage

//欢迎页面
class FYWelComeViewController: UIViewController {

   //MARK:-懒加载控件
    
    //背景图片
    
    private  lazy  var  bgImageView: UIImageView  = UIImageView(image:UIImage(named: "ad_background"))
    
    //头像
    private lazy var  headImageView :UIImageView = {
    
        let  imageView  = UIImageView(image: UIImage(named:"avatar_default_big"))
        
        //修改圆角
        
       imageView.layer.cornerRadius = 45
        
        //剪切掉多余的图片
        
        imageView.layer.masksToBounds  = true
        
        return imageView
        
        
    }()
    
    //欢迎信息
    
    private  lazy  var  messageLabel: UILabel = {
    
        let  label = UILabel()
        
        label.textColor  = UIColor.darkGrayColor()
        
        label.font  = UIFont.systemFontOfSize(14)
        
        //如果有头像  我们服务端提供的头像
        
        if  let  avatar_large  = FYUserAccountViewModel.sharedUserAccount.userAccount?.avatar_large {
        
        self.headImageView.sd_setImageWithURL(NSURL(string: avatar_large),placeholderImage: UIImage(named: "avatar_default_big"))
         }
    
        if  let  name  = FYUserAccountViewModel.sharedUserAccount.userAccount?.name {
        
        label.text = "欢迎回来\(name)"
 
        }else  {
        
        label.text = "欢迎回来"
        }
    
        
        return label
    }()
    
    override  func  loadView() {
        
        view  = bgImageView
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
    }
    
    //页面已经显示出来  执行动画
    
    override  func  viewDidAppear(animated: Bool) {
        
        super.viewDidAppear(animated)
        
        startAnimation()
    }
    
    //执行动画
    
    private  func  startAnimation() {
    
        //更新约束
        //设置透明度
        
        messageLabel.alpha  = 0
        
        headImageView.snp_makeConstraints { (make) in
            
            make.top.equalTo(view).offset(100)
        }
        
    UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: { () -> Void in
        
        self.view.layoutIfNeeded()
        }) { (_) in
            
            UIView.animateWithDuration(1, animations: { 
                
                self.messageLabel.alpha  = 1
                }, completion: { (_) in
                    
                NSNotificationCenter.defaultCenter().postNotificationName(SwitchRootVCNotification, object: nil)
            })
        }
    }
    
    //添加视图  设置约束
    
    private  func  setupUI() {
    
        view.addSubview(headImageView)
        
        view.addSubview(messageLabel)
    
    headImageView.snp_makeConstraints { (make) in
        
        make.centerX.equalTo(view)
        
        make.top.equalTo(view).offset(200)
        
        make.size.equalTo(CGSize(width: 90, height: 90))
        
         }
    
        messageLabel.snp_makeConstraints { (make) in
            
            make.centerX.equalTo(headImageView)
            
            make.top.equalTo(headImageView.snp_bottom).offset(10)
        }
    }
    

}


